package cn.tedu.ttl.front.content.service.impl;


import cn.tedu.ttl.common.enumerator.ServiceCode;
import cn.tedu.ttl.common.ex.ServiceException;
import cn.tedu.ttl.common.pojo.vo.PageData;
import cn.tedu.ttl.front.content.dao.persist.repository.IArticleRepository;
import cn.tedu.ttl.front.content.pojo.vo.ArticleListItemVO;
import cn.tedu.ttl.front.content.pojo.vo.ArticleStandardVO;
import cn.tedu.ttl.front.content.service.IArticleService;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.beans.factory.annotation.Value;
import org.springframework.stereotype.Service;

/**
 * 处理文章数据的业务实现类
 *
 * @author java@tedu.cn
 * @version 3.0
 */
@Slf4j
@Service
public class ArticleServiceImpl implements IArticleService {

    @Value("${ttl.dao.default-query-page-size}")
    private Integer defaultQueryPageSize;
    @Autowired
    private IArticleRepository articleRepository;

    public ArticleServiceImpl() {
        log.debug("创建业务类对象：ArticleServiceImpl");
    }

    @Override
    public ArticleStandardVO getStandardById(Long id) {
        log.debug("开始处理【根据ID查询文章】的业务，参数：{}", id);
        ArticleStandardVO queryResult = articleRepository.getStandardById(id);
        if (queryResult == null) {
            String message = "查询文章详情失败，文章数据不存在！";
            log.warn(message);
            throw new ServiceException(ServiceCode.ERROR_NOT_FOUND, message);
        }
        return queryResult;
    }

    @Override
    public PageData<ArticleListItemVO> listByRecommend(Integer pageNum) {
        log.debug("开始处理【查询推荐的文章列表】的业务，页码：{}", pageNum);
        return articleRepository.listByRecommend(pageNum, defaultQueryPageSize);
    }

    @Override
    public PageData<ArticleListItemVO> listByRecommend(Integer pageNum, Integer pageSize) {
        log.debug("开始处理【查询推荐的文章列表】的业务，页码：{}，每页记录数：{}", pageNum, pageSize);
        return articleRepository.listByRecommend(pageNum, pageSize);
    }

    @Override
    public PageData<ArticleListItemVO> listByCategory(Long categoryId, Integer pageNum) {
        log.debug("开始处理【根据类别查询文章列表】的业务，文章类别：{}, 页码：{}", categoryId, pageNum);
        return articleRepository.listByCategory(categoryId, pageNum, defaultQueryPageSize);
    }

    @Override
    public PageData<ArticleListItemVO> listByCategory(Long categoryId, Integer pageNum, Integer pageSize) {
        log.debug("开始处理【根据类别查询文章列表】的业务，文章类别：{}, 页码：{}，每页记录数：{}", categoryId, pageNum, pageSize);
        return articleRepository.listByCategory(categoryId, pageNum, pageSize);
    }

}
